package com.hcbd.tdwt.ui.fuda.setting.activity;



import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.hcbd.tdwt.base.GlobalParams;

import com.hcbd.tdwt.ui.xingyu.service.LocationReportService;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_position_reporting)
public class PositionReportingActivity extends BaseActivity {

    @ViewById
    TextView title;  // 主标题
    @ViewById
    EditText centerNumberET;  // 输入中心号码
    @ViewById
    RelativeLayout oneMinuteRL;  // 1分钟上报一次
    @ViewById
    RelativeLayout twoMinuteRL;  // 3分钟上报一次
    @ViewById
    RelativeLayout threeMinuteRL;  // 5分钟上报一次
    @ViewById
    RelativeLayout fourMinuteRL;  // 30分钟上报一次
    @ViewById
    TextView oneMinuteText;  // 1分钟上报一次文本
    @ViewById
    TextView oneMinuteText_2;  // 1分钟上报一次文本2
    @ViewById
    ImageView oneMinuteIconIV;  // 1分钟上报一次图标
    @ViewById
    TextView twoMinuteText;  // 3分钟上报一次文本
    @ViewById
    ImageView twoMinuteIconIV;  // 3分钟上报一次图标
    @ViewById
    TextView threeMinuteText;  // 5分钟上报一次文本
    @ViewById
    ImageView threeMinuteIconIV;  // 5分钟上报一次图标
    @ViewById
    TextView fourMinuteText;  // 30分钟上报一次文本
    @ViewById
    ImageView fourMinuteIconIV;  // 30分钟上报一次图标
    @ViewById
    TextView startTV;  // 启动按键
    private int index=60;
    private Drawable ovaBlueDrawable;
    private Drawable ovaGrayDrawable;
    private Drawable ovaRedDrawable;

    @AfterViews
    protected void init() {
        ovaBlueDrawable=getResources().getDrawable(R.drawable.button_blue_300_bg);
        ovaRedDrawable=getResources().getDrawable(R.drawable.ova_red_bg2);
        ovaGrayDrawable=getResources().getDrawable(R.drawable.ova_gray_bg);
        title.setText("位置上报");
        centerNumberET.setText(GlobalParams.rdCentreNumber);
        centerNumberET.setFocusable(false);
        if(isServiceWork("com.all.tdwt.ui.xingyu.service.LocationReportService")){
            setStartBtn(ovaRedDrawable,"暂停");
        }else{
            if(BeidouBoxParams.isBoxConnectNormal){
                setStartBtn(ovaBlueDrawable,"启动");
            }else{
                setStartBtn(ovaGrayDrawable,"未连接");
            }
        }
    }





    // 返回按键
    @Click
    void headLeft(){
        finish();
    }

    // 1分钟上报一次
    @Click
    void oneMinuteRL(){
        oneMinuteText.setTextColor(getColor(R.color.blue_300));
        oneMinuteText_2.setTextColor(getColor(R.color.blue_300));
        oneMinuteIconIV.setVisibility(View.VISIBLE);

        twoMinuteText.setTextColor(getColor(R.color.gray_200));
        twoMinuteIconIV.setVisibility(View.GONE);

        threeMinuteText.setTextColor(getColor(R.color.gray_200));
        threeMinuteIconIV.setVisibility(View.GONE);

        fourMinuteText.setTextColor(getColor(R.color.gray_200));
        fourMinuteIconIV.setVisibility(View.GONE);
        index=60;
    }

    // 3分钟上报一次
    @Click
    void twoMinuteRL(){

        oneMinuteText.setTextColor(getColor(R.color.gray_200));
        oneMinuteText_2.setTextColor(getColor(R.color.gray_200));
        oneMinuteIconIV.setVisibility(View.GONE);

        twoMinuteText.setTextColor(getColor(R.color.blue_300));
        twoMinuteIconIV.setVisibility(View.VISIBLE);

        threeMinuteText.setTextColor(getColor(R.color.gray_200));
        threeMinuteIconIV.setVisibility(View.GONE);

        fourMinuteText.setTextColor(getColor(R.color.gray_200));
        fourMinuteIconIV.setVisibility(View.GONE);
        index=180;
    }
    // 5分钟上报一次
    @Click
    void threeMinuteRL(){

        oneMinuteText.setTextColor(getColor(R.color.gray_200));
        oneMinuteText_2.setTextColor(getColor(R.color.gray_200));
        oneMinuteIconIV.setVisibility(View.GONE);

        twoMinuteText.setTextColor(getColor(R.color.gray_200));
        twoMinuteIconIV.setVisibility(View.GONE);

        threeMinuteText.setTextColor(getColor(R.color.blue_300));
        threeMinuteIconIV.setVisibility(View.VISIBLE);

        fourMinuteText.setTextColor(getColor(R.color.gray_200));
        fourMinuteIconIV.setVisibility(View.GONE);
        index=300;
    }
    // 30分钟上报一次
    @Click
    void fourMinuteRL(){

        oneMinuteText.setTextColor(getColor(R.color.gray_200));
        oneMinuteText_2.setTextColor(getColor(R.color.gray_200));
        oneMinuteIconIV.setVisibility(View.GONE);

        twoMinuteText.setTextColor(getColor(R.color.gray_200));
        twoMinuteIconIV.setVisibility(View.GONE);

        threeMinuteText.setTextColor(getColor(R.color.gray_200));
        threeMinuteIconIV.setVisibility(View.GONE);

        fourMinuteText.setTextColor(getColor(R.color.blue_300));
        fourMinuteIconIV.setVisibility(View.VISIBLE);
        index=1800;
    }

    @Click
    void startTV(){
        if (!BeidouBoxParams.isBoxConnectNormal) {
            toast("使用此功能请先开启北斗服务");
            return;
        }
        if(isServiceWork("com.all.tdwt.ui.xingyu.service.LocationReportService")){
            showOldProtocolDialog("当前上报服务正在运行,是否关闭？", (dialog, confirm) -> {
                if(confirm){
                     this.stopService(new Intent(this, LocationReportService.class));
                    setStartBtn(ovaBlueDrawable,"启动");
                    dialog.dismiss();
                }
            });
            return;
        }
        GlobalParams.locationReportFrequency=index;
        Intent mIntent = new Intent(this, LocationReportService.class);
        startService(mIntent);
        setStartBtn(ovaRedDrawable,"暂停");
        toast("已启动!");
    }
    public void setStartBtn(Drawable drawable, String text){
        startTV.setText(text);
        startTV.setBackground(drawable);
    }
}